package yt.demo.appcommontools.utils;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;

import java.util.HashMap;
import java.util.Map;

public class StringUtils {

    public static boolean isJson(String str) {
        try {
            JSONUtil.parseObj(str);
            return true;
        } catch (Exception e) {
            return false;
        }
    }

    public static Map<String, Object> splitToMap(String input, String split1, String split2) {
        Map<String, Object> map = new HashMap<>();

        // Split the input string into key-value pairs
        String[] pairs = StrUtil.split(input, split1).toArray(new String[0]);

        for (String pair : pairs) {
            String[] keyValue = StrUtil.split(pair, split2).toArray(new String[0]);
            if (keyValue.length == 2) {
                String key = keyValue[0];
                String value = keyValue[1];

                Object valueObj = Convert.convert(Object.class, value);

                map.put(key, valueObj);
            }
        }
        return map;
    }
}
